#!/bin/bash
set -e

# Check if the script is run as root
if [ "$(id -u)" -ne 0 ]; then
  echo "This script must be run as root. Please use 'sudo ./install_deps.sh'" >&2
  exit 1
fi

# Get the absolute path to the directory containing this script
SCRIPT_DIR=$(dirname "$(realpath "$0")")
DEB_DIR="$SCRIPT_DIR/deb_packages"

echo "--- Starting Offline Dependencies Installation ---"

if [ ! -d "$DEB_DIR" ]; then
    echo "Error: Directory '$DEB_DIR' not found." >&2
    exit 1
fi

echo "Installing all .deb packages from '$DEB_DIR'..."
dpkg -i "$DEB_DIR"/*.deb

# If dpkg failed, it might be due to ordering, try to fix with apt.
# The --no-download flag ensures it doesn't try to connect to the internet,
# making it safe for offline use. It only uses already unpacked .deb info.
echo "Attempting to configure any unconfigured packages..."
apt-get install -f -y --no-download

echo "Updating shared library cache..."
ldconfig

echo "--- Installation successful! ---"
